public class Z字形变换 {
    
    public static void main(String[] args) {
        
        String result = convert("PAYPALISHIRING", 3);

        System.out.println(result);

    }


    public static String convert(String s, int numRows) {
        
        if(numRows <= 1) return s;

        int len = Math.min(s.length(), numRows);

        String []rows = new String[len];
        for(int i = 0; i< len; i++) rows[i] = "";

        int loc = 0;
        boolean down = false;

        for(int i=0; i<s.length(); i++){

            rows[loc] += String.valueOf(s.charAt(i));

            if(0 == loc || loc == numRows -1){

                down = !down;
            }

            loc += down ? 1 : -1;

        }

        StringBuilder builder = new StringBuilder(s.length());

        for(String row : rows){

            builder.append(row);
        }

        return builder.toString();
    }
}
